package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.actionsheet.ActionSheet;
import com.google.zxing.integration.android.IntentIntegrator;
import com.joooonho.SelectableRoundedImageView;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.SkillTrainActivity;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.StudentApplyReserveReturn;
import com.net.wanjian.networkhospitalmanager.bean.StudentLabReserveListResult;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.consts.LabReserveBtnConst;
import com.net.wanjian.networkhospitalmanager.fragment.skilltrain.TeacherLabReserveFragment;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.TimeDateUtils;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.observers.DisposableObserver;

/**
 * Description:
 * Created by lidongcai on 17/5/11.
 * Email:  lidongcaiocc@163.com
 */

public class TeacherReserveListAdapter extends RecyclerBaseAdapter<StudentLabReserveListResult.DataBean.CurrentLabReserveListBean, TeacherReserveListAdapter.ViewHolder> {

    public TeacherReserveListAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_my_reserve_list, parent, false);
        AutoUtils.auto(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final StudentLabReserveListResult.DataBean.CurrentLabReserveListBean d, final int position) {
        //设置实验名称
        holder.setLabNameTv(URLDecoderUtil.getDecoder(d.getLabReserveName()));

        //是否存在指导教师（0不存在，1存在）
        if (URLDecoderUtil.getDecoder(d.getIsExistGuideTeacher()).equals("0")) {
            holder.setLabIsGuideTeacherIv(false);
        } else {
            holder.setLabIsGuideTeacherIv(true);
        }
        //技能名称集合（中间以英文逗号隔开）
        holder.setLabSkillTv("练习技能："+URLDecoderUtil.getDecoder(d.getTrainTypeNameArray()));

        //设置开放到关闭的时间，需要格式化
        String openTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(d.getLabReserveOpenTime()));
        String closeTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(d.getLabReserveCloseTime()));
        String ot = openTime.split(" ")[0];
        String ct = closeTime.split(" ")[0];
        if (ot.equals(ct)) {
            holder.setLabStartTimeTv(openTime + "—" + closeTime.split(" ")[1]);
        } else {
            holder.setLabStartTimeTv(openTime + "—" + closeTime);
        }

        //预约按钮的状态
        holder.setLabReserveBtn(LabReserveBtnConst.RECEIPT);


        holder.labReserveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回执弹出dialog
                ActionSheet.createBuilder(context, ((SkillTrainActivity) context).getSupportFragmentManager())
                        .setCancelButtonTitle("取消")
                        .setOtherButtonTitles("参加", "不参加", "更换老师")
                        .setCancelableOnTouchOutside(true)
                        .setListener(new ActionSheet.ActionSheetListener() {
                            @Override
                            public void onDismiss(ActionSheet actionSheet, boolean b) {

                            }

                            @Override
                            public void onOtherButtonClick(ActionSheet actionSheet, int i) {
                                switch (i) {
                                    case 0:
                                        ProgressDialogUtils.showProgressDialog(context, null, R.string.loading_text);
                                        HttpUtil.teacherReplyLabReserve(SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(),
                                                SharedXmlUtil.getInstance().getHospitalId(), SharedXmlUtil.getInstance().getUserIdentityId(), d.getLabReserveID(),
                                                "1", new DisposableObserver<StudentApplyReserveReturn>() {
                                                    @Override
                                                    public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {
                                                        if (studentApplyReserveReturn.getResult().equals(ErrorCodeReturn.success + "")) {
                                                            ToastUtil.showToast("参加成功");
                                                        }
                                                    }

                                                    @Override
                                                    public void onError(Throwable e) {
                                                        ProgressDialogUtils.dismissProgressDialog();
                                                    }

                                                    @Override
                                                    public void onComplete() {
                                                        ProgressDialogUtils.dismissProgressDialog();
                                                    }
                                                });
                                        break;
                                    case 1:
                                        ProgressDialogUtils.showProgressDialog(context, null, R.string.loading_text);
                                        HttpUtil.teacherReplyLabReserve(SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(),
                                                SharedXmlUtil.getInstance().getHospitalId(), SharedXmlUtil.getInstance().getUserIdentityId(), d.getLabReserveID(),
                                                "2", new DisposableObserver<StudentApplyReserveReturn>() {
                                                    @Override
                                                    public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {
                                                        if (studentApplyReserveReturn.getResult().equals(ErrorCodeReturn.success + "")) {
                                                            ToastUtil.showToast("取消参加成功");
                                                        }
                                                    }

                                                    @Override
                                                    public void onError(Throwable e) {
                                                        ProgressDialogUtils.dismissProgressDialog();
                                                    }

                                                    @Override
                                                    public void onComplete() {
                                                        ProgressDialogUtils.dismissProgressDialog();
                                                    }
                                                });
                                        break;
                                    case 2:
                                        ((SkillTrainActivity) context).scanQRcode(d.getLabReserveID());
                                        break;
                                }

                            }
                        }).show();

            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lab_name_tv)
        TextView labNameTv;
        @BindView(R.id.lab_skill_tv)
        TextView labSkillTv;
        @BindView(R.id.lab_start_time_tv)
        TextView labStartTimeTv;
        @BindView(R.id.lab_is_guide_teacher_iv)
        SelectableRoundedImageView labIsGuideTeacherIv;
        @BindView(R.id.relativeLayout)
        RelativeLayout relativeLayout;
        @BindView(R.id.lab_reserve_btn)
        Button labReserveBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        public void setLabNameTv(String labNameTv) {
            this.labNameTv.setText(labNameTv);
        }

        public void setLabSkillTv(String labSkillTv) {
            this.labSkillTv.setText(labSkillTv);
        }

        public void setLabStartTimeTv(String labStartTimeTv) {
            this.labStartTimeTv.setText(labStartTimeTv);
        }

        public void setLabIsGuideTeacherIv(boolean b) {
            if (b) {
                labIsGuideTeacherIv.setBackgroundResource(R.mipmap.practice_btn_green);
            } else {
                labIsGuideTeacherIv.setBackgroundResource(R.mipmap.practice_btn_grey);
            }
        }


        public void setLabReserveBtn(int type) {
            switch (type) {
                case LabReserveBtnConst.NO_RESERVED:
                    labReserveBtn.setVisibility(View.GONE);
                    break;
                case LabReserveBtnConst.UNABLE_RESERVED:
                    labReserveBtn.setEnabled(false);
                    labReserveBtn.setText("立即预约");
                    labReserveBtn.setBackgroundResource(R.drawable.lab_reserve_btn_color);
                    break;
                case LabReserveBtnConst.CAN_RESERVED:
                    labReserveBtn.setEnabled(true);
                    labReserveBtn.setText("立即预约");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    break;
                case LabReserveBtnConst.CANCEL_RESERVED:
                    labReserveBtn.setEnabled(true);
                    labReserveBtn.setText("取消预约");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    labReserveBtn.setSelected(true);
                    break;
                case LabReserveBtnConst.WAIT_OPEN:
                    labReserveBtn.setEnabled(false);
                    labReserveBtn.setText("等待开放");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    break;
                case LabReserveBtnConst.ENTER_LAB:
                    labReserveBtn.setEnabled(true);
                    labReserveBtn.setText("进入实验室");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    break;
                case LabReserveBtnConst.SERVER_CANCEL:
                    labReserveBtn.setEnabled(false);
                    labReserveBtn.setText("服务器已取消");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    break;
                case LabReserveBtnConst.RECEIPT:
                    labReserveBtn.setEnabled(true);
                    labReserveBtn.setText("回执");
                    labReserveBtn.setBackgroundResource(R.drawable.my_reserve_btn_color);
                    break;
            }
        }
    }
}
